/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.TCube;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;


import javax.faces.application.FacesMessage;  
import javax.faces.context.FacesContext;  
  
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.event.map.MarkerDragEvent;  
import org.primefaces.model.map.DefaultMapModel;  
import org.primefaces.model.map.LatLng;  
import org.primefaces.model.map.LatLngBounds;
import org.primefaces.model.map.MapModel;  
import org.primefaces.model.map.Marker; 

/**
 *
 * @author Randall Chia
 */
@ManagedBean (name="mapBean")
@ViewScoped
public class GoogleMapMB {
    private MapModel draggableModel; 
    
    private LatLng coord1;
    private Marker marker;
    
    private double latitude = 0.0;
    private double longitude = 0.0;
    
    /** Creates a new instance of GoogleMapMB */
    public GoogleMapMB() {
        draggableModel = new DefaultMapModel();
        
        coord1 = new LatLng(1.360803,103.817368);     
        draggableModel.addOverlay(new Marker(coord1, "Your Outlet")); 

        
        for(Marker m : draggableModel.getMarkers()) {  
            m.setDraggable(true);  
        }  
    }
    
    
     public MapModel getDraggableModel() {  
        return draggableModel;  
    }  
      
    public void onMarkerDrag(MarkerDragEvent event) {  
        marker = (Marker) event.getMarker(); 
        
        latitude = getMarker().getLatlng().getLat();
        longitude = getMarker().getLatlng().getLng();
        
        if(longitude < 103.593407 || longitude > 104.045219 || latitude < 1.235865 || latitude > 1.473035){
            System.out.println("Outside boundary!");
            addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Marker Dragged", "Marker outside Singapore Boundaries!"));
            marker.setLatlng(coord1);
        }

        System.out.println("Lat: " + getLatitude() + " Long: " + getLongitude());
        addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Marker Dragged", "Lat:" + getMarker().getLatlng().getLat() + ", Lng:" + getMarker().getLatlng().getLng()));  
    }  
    
    
    
    public void setMarker(PointSelectEvent event){
        LatLng latlng = event.getLatLng();
        latitude = latlng.getLat();
        longitude = latlng.getLng();
    }
    
    
    
    
    
      
    public void addMessage(FacesMessage message) {  
        FacesContext.getCurrentInstance().addMessage(null, message);  
    }  
    
    
    
    
    //Get and Set

    /**
     * @param draggableModel the draggableModel to set
     */
    public void setDraggableModel(MapModel draggableModel) {
        this.draggableModel = draggableModel;
    }

    /**
     * @return the coord1
     */
    public LatLng getCoord1() {
        return coord1;
    }

    /**
     * @param coord1 the coord1 to set
     */
    public void setCoord1(LatLng coord1) {
        this.coord1 = coord1;
    }

    /**
     * @return the marker
     */
    public Marker getMarker() {
        return marker;
    }

    /**
     * @param marker the marker to set
     */
    public void setMarker(Marker marker) {
        this.marker = marker;
    }

    /**
     * @return the latitude
     */
    public double getLatitude() {
        return latitude;
    }

    /**
     * @param latitude the latitude to set
     */
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    /**
     * @return the longitude
     */
    public double getLongitude() {
        return longitude;
    }

    /**
     * @param longitude the longitude to set
     */
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
    
    
    
    
    
}
